package com.hspedu.reflection;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * 通过反射获取类的结构信息
 */
public class ReflectionUtils {
    public static void main(String[] args) {

    }

    @Test
    public void api_02() throws ClassNotFoundException {
        //得到 Class 对像
        Class<?> cls = Class.forName("com.hspedu.reflection.Person");
        //getDeclareFields：获取本类中的所有属性
        //默认修饰符值：0   public：1    protect：4   private：2    static：8   final：16
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println("getDeclareFields=>" + declaredField.getName() + " 该属性的修饰符值：" + declaredField.getModifiers() + " 该属性的类型=》" + declaredField.getType());
        }

        //
        Method[] declaredMethods = cls.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println("getDeclaredMethods=>" + declaredMethod.getName() + " 该方法返回类型" + declaredMethod.getReturnType());

            Class<?>[] parameterTypes = declaredMethod.getParameterTypes();
            for (Class<?> parameterType : parameterTypes) {
                System.out.println("该方法的形参类型=>" + parameterType);
            }
        }
    }

    @Test
    public void api_01() throws ClassNotFoundException {
        //得到 Class 对像
        Class<?> cls = Class.forName("com.hspedu.reflection.Person");
        //getName:得到全类名
//        System.out.println(cls.getName());
        //getSimpleName:获取简单类名
//        System.out.println(cls.getSimpleName());
        //getFields:获取所有public 修饰的属性，包含本类以及父类IDE
        Field[] fields = cls.getFields();
        for (Field field : fields) {
//            System.out.println("fields=>" + field.getName());
        }
        //getDeclareFields：获取本类中的所有属性
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field declaredField : declaredFields) {
//            System.out.println("getDeclareFields=>"+declaredField.getName());
        }
        //
        Method[] methods = cls.getMethods();
        for (Method method : methods) {
//            System.out.println("getMethods=>"+method.getName());
        }
        //
        Method[] declaredMethods = cls.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
//            System.out.println("getDeclaredMethods=>"+declaredMethod.getName());
        }
        //获取本类public修饰的构造器
        Constructor<?>[] constructors = cls.getConstructors();
        for (Constructor<?> constructor : constructors) {
//            System.out.println("getConstructors=>" + constructor.getName());
        }
        //
        Constructor<?>[] declaredConstructors = cls.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
//            System.out.println("getDeclaredConstructors=>" + declaredConstructor.getName());
        }
        //
        Package aPackage = cls.getPackage();
//        System.out.println("getPackage=>" + aPackage);
        //
        Class<?> superclass = cls.getSuperclass();
//        System.out.println(superclass);
        //
        Class<?>[] interfaces = cls.getInterfaces();
        for (Class<?> anInterface : interfaces) {
//            System.out.println("getInterfaces=>" + anInterface);
        }
        //
        Annotation[] annotations = cls.getAnnotations();
        for (Annotation annotation : annotations) {
//            System.out.println("annotation=>" + annotation);
        }
    }
}

class A {
    public String hobby;

    public void hi() {
    }

    public A() {
    }
}

interface IA {
}

interface IB {
}

@Deprecated
class Person extends A implements IA, IB {
    public String name;
    protected static int age;
    String job;
    private double sal;

    public Person() {
    }

    public Person(String name) {
    }

    private Person(String name, int age) {
    }

    public void m1(String name, int age, double sal) {
    }

    protected String m2() {
        return null;
    }

    void m3() {
    }

    private void m4() {
    }
}
